package com.longrise.android.byjk.plugins.main;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.Global;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.common.FolderConstants;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.PrintLog;
import com.longrise.android.byjk.utils.Tracker_Google;
import com.longrise.android.util.Util;
import com.longrise.android.widget.LButtonBg;
import com.longrise.android.widget.LLinearLayoutView;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class UpdataAPK implements Callback, OnKeyListener {
    protected static final String TAG = "UpdataAPK";
    private Context context = null;
    private Handler handler = null;
    //APP名称
    private String appName = null;

    //回调接口
    private CheckVersionListener checkVersionListener;
    //标识符,为0是为手动检测版本
    private int mark = -1;
    private String _apkurl = null;
    private File _apkfile = null;
    private DownLoadManager _dm = null;
    private DownloadDialog bar;
    private Dialog _dialog2;
    private boolean FLAG_CAN_SHOW_DIALOG;

    SharedPreferences _sp = null;

    public UpdataAPK(Context context, String appName, boolean flag_can_show_dialog) {
        if (null == context || null == appName || "".equals(appName)) {
            return;
        }
        this.context = context;
        this.appName = appName;
        handler = new Handler(this);
        if (null == this._sp && null != this.context) {
            this._sp = this.context.getSharedPreferences(Global.getInstance().getSPName(), Context.MODE_PRIVATE);
        }
        this.FLAG_CAN_SHOW_DIALOG = flag_can_show_dialog;
    }

    public void setMark(int mark) {
        this.mark = mark;
    }

    public void setCheckVersionListener(CheckVersionListener checkVersionListener) {
        this.checkVersionListener = checkVersionListener;
    }

    @Override
    public boolean handleMessage(Message message) {
        if (null == message) {
            return false;
        }
        switch (message.what) {
            case 100:
                if (null != checkVersionListener) {
                    checkVersionListener.onCheckVersion(-1);
                }
                Bundle bundle = message.getData();
                if (null == bundle) {
                    return false;
                }

//			if(null != context)
//			{
//				if(mark != 0)
//				{
//					SharedPreferences sharedPreferences = context.getSharedPreferences(Global.getInstance().getSPName(),Context.MODE_PRIVATE);
//					long lastUpdateTime = sharedPreferences.getLong("lastUpdateTime", 0);
//					//判断是否需要进行检测
//					if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis())
//					{
//			            lastUpdateTime = System.currentTimeMillis();
//			            SharedPreferences.Editor editor = context.getSharedPreferences(Global.getInstance().getSPName(),Context.MODE_PRIVATE).edit();
//			            editor.putLong("lastUpdateTime", lastUpdateTime);
//			            editor.commit();
//					}
//					else
//					{
//						return false;
//					}
//				}
//			}

                //"1"标识强制升级
                String upgrade = bundle.getString("upgrade");
                String apkurl = bundle.getString("apkurl");
                String remarks = bundle.getString("remarks");
                String description = bundle.getString("description");
                PrintLog.d(TAG, "apkurl2:" + apkurl);
                showDialog(upgrade, apkurl, remarks, description);

                break;
            case 101:   //请求失败 点击取消更新
                if (null != checkVersionListener) {
                    checkVersionListener.onCheckVersion(-2);
                }
                break;
            case 102:  //版本不需要更新
                if (null != checkVersionListener) {
                    checkVersionListener.onCheckVersion(0);
                }
                break;
            case 103:
                installApk();
                break;
            case 104:
                checkVersion();
                break;
        }
        return false;
    }

    /**
     * xkj
     * 带更新说明的dialog
     *
     * @param upgrade     是否强制更新
     * @param apkUrl      apk下载的地址
     * @param remarks     更新说明
     * @param description 版本名称
     */
    private void showDialog(String upgrade, final String apkUrl, String remarks, String description) {
        if (context == null || TextUtils.isEmpty(upgrade) || TextUtils.isEmpty(apkUrl) || TextUtils.isEmpty(description)) {
            return;
        }
        PrintLog.d(TAG, "APKURL::" + _apkurl);
        View view = View.inflate(context, R.layout.dialog_update_tips, null);
        TextView tvTitle = (TextView) view.findViewById(R.id.dialog_update_tv_title);
        //ListView lvContent = (ListView) view.findViewById(R.id.dialog_update_lv);
        LinearLayout ll = (LinearLayout) view.findViewById(R.id.dialog_update_ll);
        TextView tvCancel = (TextView) view.findViewById(R.id.dialog_update_tv_cancel);
        TextView tvConfirm = (TextView) view.findViewById(R.id.dialog_update_tv_confirm);
        TextView tvConfirmonly = (TextView) view.findViewById(R.id.dialog_update_tv_confirm_only);
        LinearLayout rlContent = (LinearLayout) view.findViewById(R.id.dialog_update_rl);

        tvTitle.setText(context.getString(R.string.newversion) + description);
        ArrayList<String> list = new ArrayList<>();
        if (remarks != null) {
            String[] split = remarks.split("；");
            PrintLog.d(TAG, "split:" + split.length);
            for (String content : split) {
                content = content.replaceAll("\\s+", "");
                list.add(content);
                TextView tv = new TextView(context);
                tv.setText(content);
                tv.setTextColor(Color.parseColor("#999999"));
                tv.setTextSize(14);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                rlContent.addView(tv, layoutParams);
            }
        }

        //UpdateAdapter adapter = new UpdateAdapter();
        // adapter.setData(list);
        //lvContent.setAdapter(adapter);


        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                if (checkVersionListener != null) {
                    checkVersionListener.onCheckVersion(-2);
                }
            }
        });

        tvConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                showdownloadDialog();
                Thread thread = new Thread(null, _doDownLoadApk, "downloadapkBackground");
                thread.start();
            }
        });

        tvConfirmonly.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                showdownloadDialog();
                Thread thread = new Thread(null, _doDownLoadApk, "downloadapkBackground");
                thread.start();
            }
        });

        if (FLAG_CAN_SHOW_DIALOG) {
            if ("1".equals(upgrade)) {
                //強制更新，隐藏取消按钮
                ll.setVisibility(View.GONE);
                tvConfirmonly.setVisibility(View.VISIBLE);
                Dialog dialog = DialogUtil.getInstance().creatAlertDialog(context, view, 266, 200);
                if(dialog!=null){
                    dialog.setCancelable(false);
                }
            } else {
                ll.setVisibility(View.VISIBLE);
                tvConfirmonly.setVisibility(View.GONE);
                DialogUtil.getInstance().creatAlertDialog(context, view, 266, 200);
            }
        }
    }

    /**
     * 设置是否能显示提示更新的dialog
     */
    public void setCanShowDialog(boolean flag_can_show_dialog) {
        this.FLAG_CAN_SHOW_DIALOG = flag_can_show_dialog;
    }

    private class UpdateAdapter extends BaseAdapter {
        private List<String> mList;

        public void setData(List<String> list) {
            this.mList = list;
        }

        @Override
        public int getCount() {
            if (mList != null) {
                return mList.size();
            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.item_update_tips, null);
                holder = new Holder();
                holder.tv = (TextView) convertView.findViewById(R.id.item_update_text_view);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            holder.tv.setText(mList.get(position));
            return convertView;
        }
    }

    class Holder {
        TextView tv;
    }

    /**
     * 该业务已上传，提示是否继续上传
     */
    private void showDialog(String upgrade, final String apkUrl, String remarks) {
        if (null == context || null == upgrade || "".equals(upgrade) || null == apkUrl || "".equals(apkUrl)) {
            return;
        }

        //自定义对话框
        final Dialog exitDialog = new Dialog(context, R.style.dialog_style);
        exitDialog.setCanceledOnTouchOutside(false);
        exitDialog.setCancelable(false);


        //==================对话框布局==================
        int roundRadius = Util.dip2px(context, 10);
        LLinearLayoutView dialogBody = new LLinearLayoutView(context);
        dialogBody.setOrientation(LinearLayout.VERTICAL);
        dialogBody.setBackgroundColor(Color.parseColor("#287AC4"), Color.parseColor("#287AC4"), Color.parseColor("#287AC4"), Color.parseColor("#287AC4"), Color.parseColor("#287AC4"), roundRadius, 0, Color.parseColor("#287AC4"));

        //标题
        int pad = Util.dip2px(context, 10);
        TextView setTitle = new TextView(context);
        setTitle.setGravity(Gravity.CENTER);
        setTitle.setText("版本更新");
        setTitle.setTextSize(17);
        setTitle.setTextColor(Color.WHITE);
        setTitle.setPadding(0, pad, 0, pad);
        dialogBody.addView(setTitle, android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);

        //------------主体-------------
        LLinearLayoutView bodyLayout = new LLinearLayoutView(context);
        bodyLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        bodyLayout.setOrientation(LinearLayout.VERTICAL);
        bodyLayout.setBackgroundColor2(Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, new float[]{0, 0, 0, 0, roundRadius, roundRadius, roundRadius, roundRadius}, 0, Color.parseColor("#55E0E0E0"));
        dialogBody.addView(bodyLayout, (int) (Util.getScreenWidth(context) * 0.7), android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);

        int padNoticeStr = Util.dip2px(context, 20);
        EditText editText = null;

        TextView noticeStr = new TextView(context);
        noticeStr.setPadding(0, padNoticeStr, 0, padNoticeStr);
        noticeStr.setBackgroundColor(Color.WHITE);
        noticeStr.setGravity(Gravity.CENTER);
        noticeStr.setText("有新版本，请及时更新。");
        noticeStr.setTextSize(17);
        noticeStr.setTextColor(Color.parseColor("#AA000000"));
        LinearLayout.LayoutParams noticeStrLP = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        noticeStrLP.weight = 1;
        bodyLayout.addView(noticeStr, noticeStrLP);
        noticeStrLP = null;

        //分隔线
        View line = new View(context);
        line.setBackgroundColor(Color.parseColor("#E0E0E0"));
        bodyLayout.addView(line, LayoutParams.MATCH_PARENT, 1);

        LinearLayout btnLayout = new LinearLayout(context);
        bodyLayout.addView(btnLayout, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        float[] myRoundRadius = null;
        if ("1".equals(upgrade)) {
            //強制更新，隐藏取消按钮
            myRoundRadius = new float[]{0, 0, 0, 0, roundRadius, roundRadius, roundRadius, roundRadius};
        } else {
            myRoundRadius = new float[]{0, 0, 0, 0, roundRadius, roundRadius, 0, 0};

            //选择安装，不隐藏取消按钮
            LButtonBg cancelBtn = new LButtonBg(context);
            cancelBtn.setBackgroundColor2(Color.WHITE, Color.parseColor("#E0E0E0"), Color.parseColor("#E0E0E0"), -10, Color.parseColor("#E0E0E0"), new float[]{0, 0, 0, 0, 0, 0, roundRadius, roundRadius}, 0, Color.parseColor("#E0E0E0"));
            cancelBtn.setText("取消");
            cancelBtn.setTextColor(Color.parseColor("#AA000000"));
            cancelBtn.setTextSize(15);
            LinearLayout.LayoutParams cancelLP = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, Util.dip2px(context, 45));
            btnLayout.addView(cancelBtn, cancelLP);
            cancelLP.weight = 1;

            cancelBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    exitDialog.cancel();
                    Tracker_Google.getInstance().addEvent("Version-update", "Update-no", "取消更新");


                }
            });

            //分隔线
            View line2 = new View(context);
            line2.setBackgroundColor(Color.parseColor("#E0E0E0"));
            btnLayout.addView(line2, 1, LayoutParams.MATCH_PARENT);
        }
        //确定按钮
        LButtonBg setOK = new LButtonBg(context);
        setOK.setBackgroundColor2(Color.WHITE, Color.parseColor("#E0E0E0"), Color.parseColor("#E0E0E0"), -10, Color.parseColor("#E0E0E0"), myRoundRadius, 0, Color.parseColor("#E0E0E0"));
        setOK.setText("确定");
        setOK.setTextColor(Color.parseColor("#AA000000"));
        setOK.setTextSize(15);
        LinearLayout.LayoutParams setOKLP = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, Util.dip2px(context, 45));
        setOKLP.weight = 1;
        btnLayout.addView(setOK, setOKLP);
        setOK.setTag(editText);
        setOK.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                exitDialog.cancel();
                showdownloadDialog();
                Thread thread = new Thread(null, _doDownLoadApk, "downloadapkBackground");
                thread.start();
                Tracker_Google.getInstance().addEvent("Version-update", "Update-yes", "确认更新");

            }
        });
        exitDialog.setContentView(dialogBody);
        exitDialog.show();
    }

    private void showdownloadDialog() {
        if (null == context) {
            return;
        }
        View view = View.inflate(context, R.layout.download_dialog_layout, null);
        bar = (DownloadDialog) view.findViewById(R.id.download_dialog);
        _dialog2 = new Dialog(context, R.style.dialog_style);
        _dialog2.setCancelable(false);
        _dialog2.setCanceledOnTouchOutside(false);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        _dialog2.setContentView(view, lp);
        _dialog2.show();
    }

    /**
     * 检查版本更新
     */
    public void checkVersion() {
        String updateUrl = null;
        try {
            if (null == context || null == appName || "".equals(appName)) {
                if (null != handler) {
                    handler.sendEmptyMessage(101);
                }
                return;
            }

            _sp = context.getSharedPreferences(Global.getInstance().getSPName(), Context.MODE_PRIVATE);

            if (null != _sp) {
                updateUrl = _sp.getString("updateUrl", UrlConstants.getInstance().getUpdateUrl(context));
            }
            _sp = null;

            if (null == updateUrl || "".equals(updateUrl)) {
                if (null != handler) {
                    handler.sendEmptyMessage(101);
                }
                return;
            }

            EntityBean bean = new EntityBean();
            bean.set("appname", appName);
            PrintLog.d(TAG, "appName:" + appName);
            EntityBean fbean = new EntityBean();
            fbean.set("bean", bean);
            LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_getAppVersion", fbean, new LoadDataManager.OnRequestCallListener() {

                @Override
                public void onSuccess(String key, String service, Object result) {

                    EntityBean _checkVersion_eb = (EntityBean) result;

                    if (_checkVersion_eb == null) {
                        if (null != handler) {
                            handler.sendEmptyMessage(101);
                        }
                        return;
                    }

                    Integer resultstate = _checkVersion_eb.getInt("resultstate");
                    EntityBean resultbean = _checkVersion_eb.getBean("result");
                    if (resultstate != 1) {
                        if (null != handler) {
                            handler.sendEmptyMessage(101);
                        }
                        return;
                    }

                    String appversion = null;

                    appversion = resultbean.getString("appversion");
                    if (null == appversion || "".equals(appversion)) {
                        if (null != handler) {
                            handler.sendEmptyMessage(101);
                        }
                        return;
                    }

                    int iappv = Integer.parseInt(appversion);
                    Control.setLatestVersion(iappv);
                    PrintLog.d(TAG, "iappv:" + iappv);
                    PrintLog.d(TAG, "VersionCode:" + Global.getInstance().getVersionCode());
                    if (iappv > Global.getInstance().getVersionCode()) {

                        //版本描述
                        String remarks = resultbean.getString("remarks", "");
                        //版本名称
                        String description = resultbean.getString("description", "");
                        Control.setLatestVersionName(description);
                        //是否强制更新标示符
                        String upgrade = resultbean.getString("upgrade", "0");
                        //cdn完整路径
                        _apkurl = resultbean.getString("cdnpath", "");
                        PrintLog.d(TAG, "_apkurl:" + _apkurl);
                        if (_apkurl == null || "".equals(_apkurl)) {
                            _apkurl = resultbean.getString("apk", null);

                            if (null == _apkurl || "".equals(_apkurl)) {
                                if (null != handler) {
                                    handler.sendEmptyMessage(101);
                                }
                                return;
                            }
//							String string = apk_path[0];
//							if(null != string && !"".equals(string))
//							{
//								//APK 完整的下载路径
//								_apkurl = UrlConstants.getInstance().getUpdateUrl(context) + string;
//							}
                        }

                        if (null != handler) {
                            Message message = new Message();
                            Bundle bundle = new Bundle();
                            bundle.putString("remarks", remarks);
                            bundle.putString("description", description);
                            bundle.putString("upgrade", upgrade);
                            bundle.putString("apkurl", _apkurl);
                            message.setData(bundle);
                            message.what = 100;
                            bundle = null;
                            upgrade = null;
                            handler.sendMessage(message);
                        }
                    } else {
                        if (null != handler) {
                            handler.sendEmptyMessage(102);
                        }
                    }
                }

                @Override
                public void onFinished(String key, String service) {

                }

                @Override
                public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                    handler.sendEmptyMessage(101); //网络超时了也让其去登录页
                }
            });
        } catch (Exception ex) {
            if (null != handler) {
                handler.sendEmptyMessage(101);
            }
        }
    }

    public interface CheckVersionListener {
        public void onCheckVersion(int mark);
    }

    // APK下载类
    class DownLoadManager {
        private boolean isbreak = false;

        public DownLoadManager() {
        }

        public File getFileFromServer(String path, ProgressBar pd) throws Exception {
            if (null == path) return null;

            // 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                return null;
            }
            try {
                HttpURLConnection conn = null;
                URL url = new URL(path);
                if (null != url) {
                    conn = (HttpURLConnection) url.openConnection();
                    if (null != conn) {
                        conn.setConnectTimeout(5000);
                        if (200 == conn.getResponseCode()) {
                            this.isbreak = false;
                            // 获取到文件的大小
                            if (null != pd) {
                                pd.setMax(conn.getContentLength());
                            }
                            InputStream is = conn.getInputStream();
                            String locDir = FolderConstants.APK_DIR;

                            String updateApk = context.getString(R.string.updateAppFlieName);
                            if (null == updateApk) {
                                updateApk = "baobao.apk";
                            }
                            File file = new File(locDir);
                            if (null != file) {
                                if (!file.exists()) {
                                    file.mkdirs();
                                }
                            }
                            file = new File(locDir + "/" + updateApk);
                            if (null != file) {
                                if (file.exists()) {
                                    boolean delete = file.delete();
                                    PrintLog.d(TAG, "DELETE:" + delete);
                                }
                                FileOutputStream fos = new FileOutputStream(file);
                                BufferedInputStream bis = new BufferedInputStream(is);
                                byte[] buffer = new byte[1024];
                                int len;
                                int total = 0;
                                while ((len = bis.read(buffer)) != -1) {
                                    if (isbreak) {
                                        file = null;
                                        break;
                                    }
                                    fos.write(buffer, 0, len);
                                    total += len;
                                    if (null != pd) {
                                        pd.setProgress(total);
                                    }
                                }
                                fos.close();
                                bis.close();
                                is.close();
                            }

                            return file;
                        }
                    }
                }

            } catch (Exception ex) {

            }
            return null;

        }

        public void stop() {
            this.isbreak = true;
        }
    }

    // 下载数据
    private Runnable _doDownLoadApk = new Runnable() {
        @Override
        public void run() {
            try {
                _apkfile = null;
                if (null != _apkurl && !"".equals(_apkurl)) {
                    if (null == _dm) {
                        _dm = new DownLoadManager();
                    }
                    _apkfile = _dm.getFileFromServer(_apkurl, bar);
                    if (null != handler && _apkfile != null) {
                        handler.sendEmptyMessage(103);
                    }
                }
            } catch (Exception ex) {

            }
        }
    };

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (null != this._dialog2 && this._dialog2.isShowing()) {
                _dm.stop();
                this._dialog2.dismiss();
            }
        }
        return false;
    }

    private void installApk() {

        if (null != context) {
            SharedPreferences sp = context.getSharedPreferences(Global.getInstance().getSPName(), Context.MODE_PRIVATE);
            //使用默认存储位置
            sp.edit().putBoolean("isFirst", false).commit();
        }

        if (Build.VERSION.SDK_INT >= 24) {  //判断版本android7.0
            Uri apkUri = FileProvider.getUriForFile(context, FolderConstants.FILE_PROVIDER, _apkfile);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            context.startActivity(intent);
        } else { //android 7.0以下
            Intent intent = new Intent();
            // 执行动作
            intent.setAction(Intent.ACTION_VIEW);
            // 执行的数据类型
            intent.setDataAndType(Uri.fromFile(this._apkfile), "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 执行打开文件
            context.startActivity(intent);
        }

        // 将平台的IP地址清空
        if (null == this._sp) {
            this._sp = this.context.getSharedPreferences(Global.getInstance().getSPName(), Context.MODE_PRIVATE);
        }

        // 保存新的服务地址
        _sp.edit().putString("serverUrl", null).commit();
        // 更新平台的地址
        _sp.edit().putString("updateUrl", null).commit();
        if (_dialog2 != null) {
            _dialog2.dismiss();
        }
    }
}
